
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using Swaf.DataAccess;
using Swaf.Container;

namespace Swaf.Messaging
{
	/// <summary>
	/// Scott Matthews - 10.22.2004
	/// CQ ID: CUSP00005551 - EMail Messaging Provider
	/// Summary description for SmtpHelper.
	/// </summary>
	public class SmtpMessagingSystem : MessagingSystemBase
	{
		/// <summary>
		/// Memeber variable to hold the connection
		/// </summary>
		protected IMessagingConnectionInfo m_connection;

		/// <summary>
		/// Default constructor
		/// </summary>
		public SmtpMessagingSystem() {}

		#region IMessagePublisher Members
		public override void beginTrans(IMessagingConnectionInfo info)
		{
			//TODO: Log info that transactions are being used on this messaging system which does not support it.
		}

		public override void rollback(IMessagingConnectionInfo info)
		{
			//TODO: Log info that transactions are being used on this messaging system which does not support it.
		}

		public override void commit(IMessagingConnectionInfo info)
		{
			//TODO: Log info that transactions are being used on this messaging system which does not support it.
		}
		
		/// <summary>
		/// Create the message
		/// </summary>
		protected override IMessage createMessage(string msgBody, IDictionary messageProperties)
		{
			try
			{
				// Init new message
				EmailMessage message = new EmailMessage();

				// If messageProperties were provided, set them on the new Message object.
				if (messageProperties != null)
					foreach(DictionaryEntry e in messageProperties)
                        if (e.Key.ToString() == "Attachment")
                        {
                            EmailAttachment att = new EmailAttachment("",e.Value.ToString(), null);
                            message.Attachments.Add(att);
                        }
                        else
						    Cmn.callPropertySet(message, e.Key.ToString(), e.Value);

				// Populate the body of the message and connnection configuration
				message.Body = msgBody;
				message.HostName = m_connection.HostName;

				// Return a new EmailMsg
				return new EmailMsg(message, msgBody);
			}
			catch(Exception e)
			{
				//TAC handle exception
				throw e;
			}
		}

		/*
		/// <summary>
		/// Open the connection
		/// </summary>
		protected override void open(INameAddressSupport connection)
		{
			m_connection = connection;
		}
		*/

		/// <summary>
		/// Open the connection
		/// </summary>
		protected override void open(string connectionName)
		{
			try
			{
				m_connection = null;
				if (! connectionName.Equals(""))
				{
					m_connection=(IMessagingConnectionInfo) Application.currentApp.appData.get("Messaging." + connectionName);
				}
				if (m_connection == null)
				{
					throw new InvalidMessagingConnectionException(String.Format("Connection {0} was not found in the Messaging Catalog",connectionName));	
				}
			}
			catch (Exception e)
			{
				throw e;
			}
		}

		protected override void open(IMessagingConnectionInfo connection)
		{
			m_connection = connection;
		}


		/*
		private string connectionProperty(string key)
		{
			return (string) m_connection.get(key);
		}
		*/

		/// <summary>
		/// Close the connection
		/// </summary>
		protected override void close() {}

		/// <summary>
		/// Publish the message with the message object and destination
		/// </summary>
		protected override void publishMessage(IMessage objMessage, string destination)
		{
			try
			{
				// Create and send the message after setting applicable properties 
				EmailMessage message = (EmailMessage) objMessage.GetMessage();
				message.To = destination.Trim();

				// Send the message
				SmtpHelper helper = new SmtpHelper();
				helper.SendSmtpMessage(message);
			}
			catch (Exception e)
			{
				throw e;
			}
		}

		/// <summary>
		/// Publish the message with the message object, destination, reply to, and UNUSED: replyQueueManager
		/// </summary>
		protected override void publishMessage(IMessage objMessage, string destination, string replyDestination, string replyQueueManager)
		{
			try
			{	
				// Create and send the message after setting applicable properties
				EmailMessage message = (EmailMessage) objMessage.GetMessage();
				message.To = destination.Trim();
				message.ReplyTo = replyDestination.Trim();
				
				// Send the message
				SmtpHelper helper = new SmtpHelper();
				helper.SendSmtpMessage(message);
			}
			catch (Exception e)
			{
				throw e;
			}

		}

		/// <summary>
		/// Publish the reply with the message object, destination, and originalMessage
		/// </summary>		
		protected override void publishReply(IMessage objMessage, string destination, IMessage originalMessage)
		{
			try
			{
				// Create and send the message after setting applicable properties
				EmailMessage message = (EmailMessage) objMessage.GetMessage();
				message.To = message.ReplyTo;
				message.From = message.To;

				// Send the message
				SmtpHelper helper = new SmtpHelper();
				helper.SendSmtpMessage(message);
			}
			catch (Exception e)
			{
				throw e;
			}
		}

		/// <summary>
		/// Get the message.  NOT IMPLEMENTED FOR SMTP 
		/// </summary>	
		protected override IMessage GetMessage(string source, int timeoutInMS)
		{
			return null;
		}

		/// <summary>
		/// Get the message.  NOT IMPLEMENTED FOR SMTP
		/// </summary>	
		protected override IMessage GetMessage(string source, object correlationID, int timeoutInMS)
		{
			return null;
		}

		#endregion
	}
}
